package me.numark;

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

import com.baidu.frontia.api.FrontiaPushMessageReceiver;
import com.google.gson.Gson;

import java.util.List;

import me.numark.net.NumaAPIs;
import me.numark.utils.Global;
import me.numark.utils.MessageUtils;

/**
 * Created by Evan on 14/11/19.
 */
public class PushReceiver extends FrontiaPushMessageReceiver {

    private static final String LOG_TAG = "NumaPush";
    Gson mGson = new Gson();
    NotificationManager notificationManager;

    @Override
    public void onBind(final Context context, final int errorCode, String appid,
                       String userId, String channelId, String requestId) {
        if (errorCode == 0) {
            Global.BAIDU_USER_ID = userId;
            Global.BAIDU_CHANNEL_ID = channelId;
            NumaAPIs.bindPush();
        }
    }

    @Override
    public void onMessage(final Context context, String message, String customContentString) {
        Log.e(LOG_TAG, "**********Get push:" + message);
        MessageUtils.asyncUpdateMsg(context);
        if (message == null) {
            return;
        }
        PushMessage msg = mGson.fromJson(message, PushMessage.class);
        if (msg == null || (msg.custom_content != null && msg.custom_content.hide > 0)) {
            return;
        }
        int uid = 0;
        if (msg.custom_content != null) {
            uid = msg.custom_content.uid;
        }
        if (uid == Global.currentChatUid) {
            return;
        }
        Intent intent = new Intent(context, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        NotificationCompat.Builder nb = new NotificationCompat.Builder(context)
                .setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle(msg.title)
                .setContentText(msg.description)
                .setContentIntent(pendingIntent);
        if (notificationManager == null) {
            notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        }
        notificationManager.notify(uid, nb.build());
    }

    @Override
    public void onNotificationClicked(Context context, String title, String description, String customContentString) {

    }


    @Override
    public void onSetTags(Context context, int errorCode, List<String>
            successTags, List<String> failTags, String requestId) {
    }

    @Override
    public void onDelTags(Context context, int errorCode, List<String> successTags, List<String> failTags, String requestId) {
    }

    @Override
    public void onListTags(Context context, int errorCode, List<String> tags, String requestId) {
    }

    @Override
    public void onUnbind(Context context, int errorCode, String requestId) {
    }

    class CustomContent {
        public long did;
        public int uid;
        public int hide;
    }

    class PushMessage {
        public String title;
        public String description;
        public CustomContent custom_content;
    }
}
